package org.mg.core.service.impl;

import org.mg.core.dao.DBPropertyDao;
import org.mg.core.entity.DBProperty;
import org.mg.core.service.DBPropertyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MG
 * @since 15-04-2013
 */
@Service("dbPropertyService")
public class DBPropertyServiceImpl implements DBPropertyService {
    private static final Logger logger = LoggerFactory.getLogger(DBPropertyServiceImpl.class);

    @Autowired
    DBPropertyDao dbPropertyDao;

    @Override
    public Map<String, String> getAllProperties() {
        Iterable<DBProperty> properties = dbPropertyDao.findAll();

        Map<String, String> propertyMap = new HashMap<String, String>();
        for (DBProperty property : properties) {
            propertyMap.put(property.getKey(), property.getValue());
        }
        return propertyMap;
    }

    @Override
    public void saveProperty(String key, String value) {
        DBProperty property = new DBProperty();
        property.setKey(key);
        property.setValue(value);
        dbPropertyDao.save(property);
    }
}
